package br.com.iswe.gwt.ui.client;

import br.com.iswe.gwt.ui.client.img.ISWEImages;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.SourcesMouseEvents;
import com.google.gwt.user.client.ui.Widget;

public class ModalDialogBox extends ModalPopupPanel implements MouseListener {
	
	private ISWEImages isweImages = (ISWEImages)GWT.create(ISWEImages.class);
	
	private SourcesMouseEvents caption = null;

	protected Widget child;

	private boolean dragging;

	private int dragStartX, dragStartY;

	protected FlexTable panel = new FlexTable();

	/**
	 * Creates an empty dialog box. It should not be shown until its child
	 * widget has been added using {@link #add(Widget)}.
	 */
	public ModalDialogBox() {
		this(false);
	}

	/**
	 * Creates an empty dialog box specifying its "auto-hide" property. It
	 * should not be shown until its child widget has been added using
	 * {@link #add(Widget)}.
	 * 
	 * @param autoHide
	 *            <code>true</code> if the dialog should be automatically
	 *            hidden when the user clicks outside of it
	 * @param modal
	 *            <code>true</code> if keyboard and mouse events for widgets
	 *            not contained by the dialog should be ignored
	 */
	public ModalDialogBox(boolean autoHide) {
		super(autoHide);
	}
	
	public void setupDialogBox(String title) {
		FlexTable table = new FlexTable();
		
		table.setStyleName("iswe-titleBar");
		
		//icone: '>>'
		Image icone = isweImages.iconeTopico01().createImage();
		table.setWidget(0, 0, icone);
		
		//Titulo
		table.setWidget(0, 1, new Label(title));
		table.getFlexCellFormatter().setAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_MIDDLE);
		table.getFlexCellFormatter().setWidth(0, 1, "100%");

		setupDialogBox( new HTML(table.toString()));
	}
	
	public void setupDialogBox(HTML caption){
		//caption
		this.caption = caption;
		//caption.setWidth("100%");
		panel.setWidget(0, 0, caption);
		panel.getCellFormatter().setWidth(0, 0, "100%");
		
		panel.setHeight("100%");
		panel.setBorderWidth(0);
		panel.setCellPadding(0);
		panel.setCellSpacing(0);
		
		//content
		panel.getCellFormatter().setAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		
		super.setWidget(panel);
		
		panel.setStyleName("iswe-dialogBox");
		//caption.setStyleName("caption");
		
		caption.addMouseListener(this);
	}

	public boolean onEventPreview(Event event) {
		// We need to preventDefault() on mouseDown events (outside of the
		// DialogBox content) to keep text from being selected when it
		// is dragged.
		if (DOM.eventGetType(event) == Event.ONMOUSEDOWN) {
			Widget wdg = (Widget)caption;
			if (DOM.isOrHasChild(wdg.getElement(), DOM.eventGetTarget(event))) {
				DOM.eventPreventDefault(event);
			}
		}

		return super.onEventPreview(event);
	}

	public void onMouseDown(Widget sender, int x, int y) {
		dragging = true;
		Widget wdg = (Widget)caption;
		DOM.setCapture(wdg.getElement());
		dragStartX = x;
		dragStartY = y;
	}

	public void onMouseEnter(Widget sender) {
	}

	public void onMouseLeave(Widget sender) {
	}

	public void onMouseMove(Widget sender, int x, int y) {
		if (dragging) {
			int absX = x + getAbsoluteLeft();
			int absY = y + getAbsoluteTop();
			setPopupPosition(absX - dragStartX, absY - dragStartY);
		}
	}

	public void onMouseUp(Widget sender, int x, int y) {
		dragging = false;
		Widget wdg = (Widget)caption;
		DOM.releaseCapture(wdg.getElement());
	}

	public boolean remove(Widget w) {
		if (child != w) {
			return false;
		}

		panel.remove(w);
		return true;
	}

	public void setWidget(Widget w) {
		// If there is already a widget, remove it.
		if (child != null) {
			panel.remove(child);
		}

		// Add the widget to the center of the cell.
		if (w != null) {
			panel.setWidget(1, 0, w);
		}

		child = w;
	}

	/**
	 * Override, so that interior panel reflows to match parent's new width.
	 * 
	 * @Override
	 */
	public void setWidth(String width) {
		panel.getCellFormatter().setWidth(1, 0, width);
	}
	
	public void setHeight(String height) {
		panel.getCellFormatter().setHeight(1, 0, height);
	}
}
